#include "Stack.h"

void STInit(ST* ps)
{
	STDataType* tmp = (STDataType*)malloc(4 * sizeof(STDataType));
	if (tmp == NULL)
	{
		perror("malloc fail::::");
		exit(-1);
	}
	ps->a = tmp;
	ps->capacity = 4;
	ps->top = 0;
}

void StackDestroy(ST* ps)
{
	assert(ps);
	free(ps->a);
	ps->capacity = 0;
	ps->top = 0;
}

void StackPush(ST* ps, STDataType x)
{
	if (ps->top == ps->capacity)
	{
		STDataType* tmp = realloc(ps->a, 2*ps->capacity * sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("realloc fail::::");
		}
		ps->a = tmp;
		ps->capacity = 2 * ps->capacity;
	}

	ps->a[ps->top] = x;
	ps->top++;
}


void StackPop(ST* ps)
{
	assert(ps);
	ps->top--;
}


STDataType StackTop(ST* ps)
{
	assert(ps);
	assert(ps->top > 0);

	return ps->a[ps->top - 1];
}

bool StackEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}

int StackSize(ST* ps)
{
	assert(ps);
	return ps->top;
}
